package com.ruoyi.coal.modules.coalpile.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.coal.common.utils.PageUtils;
import com.ruoyi.coal.common.utils.R;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardHistoryEntity;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.CutUpEntity;
import com.ruoyi.coal.modules.coalpile.domain.vo.CoalYardInfoVo;
import com.ruoyi.coal.modules.coalpile.domain.vo.CoalYardVo;
import com.ruoyi.coal.modules.coalpile.mapper.CoalYardHistoryDao;
import com.ruoyi.coal.modules.coalpile.mapper.CoalYardInfoDao;
import com.ruoyi.coal.modules.coalpile.service.CoalYardInfoService;
import com.ruoyi.coal.modules.coalpile.service.CutUpService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "coalyardinfo", tags = {"煤场信息"})
@RestController
@RequestMapping({"coalpile/coalyardinfo"})
@DataSource(value = DataSourceType.SLAVE)
public class CoalYardInfoController {
  @Autowired
  private CoalYardInfoService coalYardInfoService;
  
  @Autowired
  private CutUpService cutUpService;
  
  @Autowired
  private CoalYardInfoDao coalYardInfoDao;
  
  @Autowired
  private CoalYardHistoryDao coalYardHistoryDao;
  
  @ApiOperation(value = "分页查询", notes = "分页查询")
  @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = "limit", value = "每页显示记录数", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "order", value = "排序方式，可选值(asc,desc)", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "key", value = "关键字", paramType = "query", dataType = "String")})
  @GetMapping({"/list"})
  @RequiresPermissions({"coalpile:coalyardinfo:list"})
  public R list(@ApiIgnore @RequestParam Map<String, Object> params) {
    PageUtils page = this.coalYardInfoService.queryPage(params);
    return R.ok().put("page", page);
  }
  
  @ApiOperation(value = "获取所有煤场信息", notes = "获取所有煤场信息")
  @GetMapping({"/getAll"})
  public R getAll() {
    List<CoalYardInfoVo> coalYardInfoList = this.coalYardInfoService.getAll();
    return R.ok().put("data", coalYardInfoList);
  }
  
  @ApiOperation(value = "通过id查询", notes = "通过id查询")
  @GetMapping({"/info/{id}"})
  @RequiresPermissions({"coalpile:coalyardinfo:info"})
  public R info(@PathVariable("id") Integer id) {
    CoalYardInfoEntity coalYardInfo = (CoalYardInfoEntity)this.coalYardInfoService.getById(id);
    return R.ok().put("coalYardInfo", coalYardInfo);
  }
  
  @ApiOperation(value = "新增煤场信息", notes = "新增煤场信息")
  @PostMapping({"/save"})
  @RequiresPermissions({"coalpile:coalyardinfo:save"})
  public R save(@RequestBody CoalYardInfoEntity coalYardInfo) {
//    if(coalYardInfo.getId())
//    coalYardInfo.setAngle(Integer.valueOf(0));
    this.coalYardInfoService.save(coalYardInfo);
    CutUpEntity cutUpEntity = new CutUpEntity();
    cutUpEntity.setCoalYardId(coalYardInfo.getId());
    cutUpEntity.setCoalPileGround(coalYardInfo.getGround());
    cutUpEntity.setName(coalYardInfo.getName());
    cutUpEntity.setCoalPileDensity(coalYardInfo.getDensity());
    cutUpEntity.setCutType(coalYardInfo.getType());
    cutUpEntity.setDel(Integer.valueOf(1));
    cutUpEntity.setMeasureType(Integer.valueOf(1));
    this.cutUpService.save(cutUpEntity);
    return R.ok();
  }
  
  @ApiOperation(value = "根据id修改煤场信息", notes = "根据id修改煤场信息")
  @PostMapping({"/update"})
  @RequiresPermissions({"coalpile:coalyardinfo:update"})
  public R update(@RequestBody CoalYardInfoEntity coalYardInfo) {
    this.coalYardInfoService.updateById(coalYardInfo);
    return R.ok();
  }
  
  @ApiOperation(value = "通过id删除煤场信息", notes = "通过id删除煤场信息")
  @PostMapping({"/delete"})
  @RequiresPermissions({"coalpile:coalyardinfo:delete"})
  public R delete(@RequestBody Integer[] ids) {
    for (Integer coalYardId : Arrays.<Integer>asList(ids)) {
      for (Object historyEntity : this.coalYardHistoryDao.selectList((Wrapper)(new QueryWrapper()).eq("coal_yard_id", coalYardId)))
        this.coalYardHistoryDao.deleteById(((CoalYardHistoryEntity)historyEntity).getId());
      this.coalYardInfoDao.deleteById(coalYardId);
    }
    return R.ok();
  }
  
  @ApiOperation(value = "获取煤场的name", notes = "获取煤场的name")
  @GetMapping({"/coalList"})
  public R getCoalYardInfo() {
    List<CoalYardVo> CoalIdList = this.coalYardInfoService.getCoalIdList();
    return R.ok().put("data", CoalIdList);
  }
  
  @ApiOperation(value = "获取煤场的name(电云导入计算)", notes = "获取煤场的name(电云导入计算)")
  @GetMapping({"/coalListImport"})
  public R getCoalYardInfoImport() {
    List<CoalYardVo> CoalIdList = this.coalYardInfoService.getCoalIdList();
    for (int i = 0; i < CoalIdList.size(); i++) {
      if (((CoalYardVo)CoalIdList.get(i)).getName().equals("导入料堆"))
        CoalIdList.remove(i); 
    } 
    return R.ok().put("data", CoalIdList);
  }
  
  @ApiOperation(value = "一键修复", notes = "一键修复")
  @GetMapping({"/repair/{id}"})
  public R repair(@PathVariable Integer id) {
    CoalYardInfoEntity byId = (CoalYardInfoEntity)this.coalYardInfoService.getById(id);
    byId.setStatus(Integer.valueOf(0));
    boolean b = this.coalYardInfoService.updateById(byId);
    return R.ok();
  }
  
  @ApiOperation(value = "根据煤场名称获取煤场信息", notes = "根据煤场名称获取煤场信息")
  @GetMapping({"/queryImportCloudInfo/{name}"})
  public R queryImportCloudInfo(@PathVariable String name) {
    CoalYardInfoEntity coalYardInfoEntity = this.coalYardInfoService.queryImportCloudInfo(name);
    return R.ok().put("data", coalYardInfoEntity);
  }
  
  @ApiOperation(value = "读取文件夹里面所有文件路径", notes = "读取文件夹里面所有文件路径")
  @GetMapping({"/getFile/{url}"})
  public R readAllFilePaths(@PathVariable String url) {
    File AllFile = this.coalYardInfoService.getALLFile(url);
    return R.ok().put("data", AllFile);
  }
  
  @ApiOperation(value = "删除文件夹下的所有文件", notes = "删除文件夹下的所有文件")
  @GetMapping({"/deleteFile/{url}"})
  public R deleteFile(@PathVariable String url) {
    this.coalYardInfoService.removeFile(url);
    return R.ok();
  }
}
